package com.lxt.cloud.testhystrix.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class HystrixService {

    //==================服务降级
    //因为Hystrix的框架用的是tomcat里的线程池
    public String paymentInfo_OK(Long id){
        return "线程池：   "+Thread.currentThread().getName()+"   paymentInfo_OK id: "+id+"\t哈哈";
    }

//    规定这个线程的超时时间是3s，3s后就由fallbackMethod指定的方法帮我“兜底”（服务降级）
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",
                    value = "12000")})
    public String paymentInfo_TimeOut(Long id){
        int timeNum = 4000;
//        int x = 10/0;
        //休眠timeNum秒钟
        try {
            TimeUnit.MILLISECONDS.sleep(timeNum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池：  "+ Thread.currentThread().getName()+"   paymentInfo_Timeout,  id:  "+id
                +"\t"+"╭(╯^╰)╮" + "耗时"+ timeNum +"毫秒";
    }
    public String paymentInfo_TimeOutHandler(Long id){
        return "线程池：  "+ Thread.currentThread().getName()+"   paymentInfo_TimeoutHandler,  id:  "+id
                +"\t"+"o(╥﹏╥)o";
    }

    //==================服务熔断
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //是否开启断路器

            //当在配置时间窗口内达到此数量的失败后，进行短路。10个/10s 注意分母是10s。 默认20个/10s
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            //短路多久以后开始尝试是否恢复，默认5s
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            //失败率达到多少后跳闸
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        if(id < 0){
            throw new RuntimeException("******id 不能为负数");
        }
        String serialNumber = UUID.randomUUID().toString();

        return Thread.currentThread().getName()+"\t"+"调用成功，流水号： "+ serialNumber;
    }
    //服务降级方法
    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
        return "id 不能为负数，请稍后再试， o(╥﹏╥)o  id: "+id;
    }

}
